package controllers.users;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mysql.jdbc.Statement;

import component.UserBis;
import hprose.HproseClinetService;
import constants.APIConstants;
import constants.Constants;
import controllers.common.filter.BaseFilter;
import play.mvc.Controller;
import utils.convert.Convert;
import utils.logs.LogsUtils;

public class UsersManageAction extends BaseFilter {
	
	/**
	 * 用户列表
	* @Title: index 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param     设定文件 
	* @return void    返回类型 
	* @author chasel 641243343@qq.com   
	* @throws
	 */
	public static void index(){
		Map<String, Object> map = new HashMap<String, Object>();
		Object [] arguments = new Object[]{map};
		map.put("customer_id", UserBis.currUser());
		map.put("operator_id", UserBis.currOperatorId());
		map.put("plat_id", UserBis.currPlatId());
		map.put("status", "1");
		String res = HproseClinetService.synCall(APIConstants.ROLEMI_GETROLESBYPLATID,arguments, APIConstants.AUTH_HOST, APIConstants.AUTH_PORT);
		JSONObject roles = JSONObject.parseObject(res);
		String data = roles.getString("data");
		JSONObject rd = JSONObject.parseObject(data);
		String role_data_str = rd.getString("role_data");
		render(role_data_str);
	}
	/**
	 * 获取用户列表
	* @Title: ajaxGetUserList 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param     设定文件 
	* @return void    返回类型 
	* @author chasel 641243343@qq.com   
	* @throws
	 */
	public static void ajaxGetUserList(){
		Map<String, Object> map = new HashMap<String, Object>();
		Object [] arguments = new Object[]{map};
		map.put("customer_id", UserBis.currUser());
		map.put("operator_id", UserBis.currOperatorId());	
		map.put("plat_id", UserBis.currPlatId());
		map.put("currPage", params.get("currPage"));
		map.put("pageSize", params.get("pageSize"));
		map.put("condition", params.get("condition"));
		String res = HproseClinetService.synCall(APIConstants.USERMI_GETUSERSLIST, arguments, APIConstants.AUTH_HOST, APIConstants.AUTH_PORT);
		renderJSON(res);
	}
	/**
	 * 获取用户详细信息
	* @Title: ajaxGetUserById 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param     设定文件 
	* @return void    返回类型 
	* @author chasel 641243343@qq.com   
	* @throws
	 */
	public static void ajaxGetUserById(){
		Map<String, Object> map = new HashMap<String, Object>();
		Object [] arguments = new Object[]{map};
		map.put("customer_id", UserBis.currUser());
		map.put("user_id", params.get("user_id"));
		map.put("operator_id", UserBis.currOperatorId());
		map.put("plat_id", UserBis.currPlatId());
		map.put("status", "1");
		String res = HproseClinetService.synCall(APIConstants.USERMI_GETUSERSBYID,arguments, APIConstants.AUTH_HOST, APIConstants.AUTH_PORT);
		String rolestr = HproseClinetService.synCall(APIConstants.ROLEMI_GETROLESBYPLATID,arguments, APIConstants.AUTH_HOST, APIConstants.AUTH_PORT);
		JSONObject roles  = JSONObject.parseObject(rolestr);
		String data = roles.getString("data");
		JSONObject rd = JSONObject.parseObject(data);
		String role_data_str = rd.getString("role_data");
		JSONArray roleArr = JSONArray.parseArray(role_data_str);
		JSONObject urs  = JSONObject.parseObject(res);
		JSONObject ur_data =  urs.getJSONObject("data");
		JSONArray urArr = ur_data.getJSONArray("role_data");
		for (int i = 0; i < roleArr.size(); i++) {
			JSONObject roleObj  = roleArr.getJSONObject(i);
			for (int j = 0; j < urArr.size(); j++) {
				JSONObject urObj = urArr.getJSONObject(j);
				if(roleObj.getString("roleId").equals(urObj.getString("roleId"))){
					roleObj.put("pick", "1");
					roleObj.put("startDate", urObj.getString("startDate"));
					roleObj.put("endDate", urObj.getString("endDate"));
					break;
				}else{
					roleObj.put("pick", "0");
				}
			}
		}
		JSONObject json = JSONObject.parseObject(res);
		JSONObject jsonData = json.getJSONObject("data");
		jsonData.put("role_data", roleArr);
		renderJSON(json);
	}
	
	/**
	 * 添加用户
	* @Title: submitUser 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param     设定文件 
	* @return void    返回类型 
	* @author chasel 641243343@qq.com   
    * @throws
	 */
	public static void submitUser(){
		Map<String, Object> map = new HashMap<String, Object>();
		Object [] arguments = new Object[]{map};
		JSONArray plats = new JSONArray();
		JSONObject plat = new JSONObject();
		plat.put("plat_id", UserBis.currPlatId());
		plat.put("customer_id", UserBis.currUser());
		plats.add(plat);
		map.put("operator_id", UserBis.currOperatorId());
		map.put("password", params.get("password"));
		map.put("userName", params.get("userName"));
		map.put("realName", params.get("realName"));
		map.put("phone", params.get("phone"));
		map.put("address", params.get("address"));
		map.put("email", params.get("email"));
		map.put("sex", params.get("sex"));
		map.put("status", params.get("status"));
		map.put("plats", plats+"");
		map.put("roles", params.get("roles"));
		LogsUtils.addLogs(Constants.LOG_TYPE_ADD,Convert.convertToInt(UserBis.currOperatorId(),-1), 1, "执行系统成员添加","success", UserBis.currLoginAccount(), UserBis.currUserName(), UserBis.currUser());
		String res = HproseClinetService.synCall(APIConstants.USERMI_ADDUSERINFO,arguments, APIConstants.AUTH_HOST, APIConstants.AUTH_PORT);
		renderJSON(res);
	}
	/**
	 * 编辑用户
	* @Title: editUserInfo 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param     设定文件 
	* @return void    返回类型 
	* @author chasel 641243343@qq.com   
	* @throws
	 */
	public static void editUserInfo(){
		Map<String, Object> map = new HashMap<String, Object>();
		JSONArray plats = new JSONArray();
		JSONObject plat = new JSONObject();
		plat.put("plat_id", UserBis.currPlatId());
		plat.put("customer_id", UserBis.currUser());
		plats.add(plat);
		Object [] arguments = new Object[]{map};
		map.put("operator_id", UserBis.currOperatorId());
		map.put("user_id", params.get("user_id"));
		map.put("realName", params.get("realName2"));
		map.put("phone", params.get("phone2"));
		map.put("address", params.get("address2"));
		map.put("email", params.get("email2"));
		map.put("sex", params.get("sex2"));
		map.put("status", params.get("status2"));
		map.put("plats", plats+"");
		map.put("roles", params.get("roles"));
		map.put("plat_id", UserBis.currPlatId());
		LogsUtils.addLogs(Constants.LOG_TYPE_ADD,Convert.convertToInt(UserBis.currOperatorId(),-1), 1, "执行系统成员编辑","success", UserBis.currLoginAccount(), UserBis.currUserName(), UserBis.currUser());
		String res = HproseClinetService.synCall(APIConstants.USERMI_EDITUSERINFO,arguments, APIConstants.AUTH_HOST, APIConstants.AUTH_PORT);
		renderJSON(res);
	}
	/**
	 * 检测用户是否存在
	* @Title: checkUserNameIsExist 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param     设定文件 
	* @return void    返回类型 
	* @author chasel 641243343@qq.com   
	* @throws
	 */
	public static void checkUserNameIsExist(){
		Map<String, Object> map = new HashMap<String, Object>();
		Object [] arguments = new Object[]{map};
		map.put("userName", params.get("userName"));
		String res = HproseClinetService.synCall(APIConstants.USERMI_CHECKUSERNAMEISEXIST,arguments, APIConstants.AUTH_HOST, APIConstants.AUTH_PORT);
		renderJSON(res);
	}
	
	public static void deleteUser(){
		Map<String, Object> map = new HashMap<String, Object>();
		Object [] arguments = new Object[]{map};
		map.put("user_id", params.get("user_id"));
		String res = HproseClinetService.synCall(APIConstants.USERMI_DELUSERS,arguments, APIConstants.AUTH_HOST, APIConstants.AUTH_PORT);
		LogsUtils.addLogs(Constants.LOG_TYPE_DEL,Convert.convertToInt(UserBis.currOperatorId(),-1), 1, "删除成员","success", UserBis.currLoginAccount(), UserBis.currUserName(), UserBis.currUser());
		renderJSON(res); 
	}

}
